package com.weiaibenpao.demo.chislim.hurui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.hengxianghuadong.PageRecyclerView;
import com.weiaibenpao.demo.chislim.map.activity.NormalOutdoorRunActivity;
import com.weiaibenpao.demo.chislim.map.activity.SportHomeActivity;
import com.weiaibenpao.demo.chislim.sport.FavouritePicActivity;
import com.weiaibenpao.demo.chislim.sport.RegionActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2016/4/22.
 */
public class SportFragment extends Fragment {

    private PageRecyclerView mRecyclerView = null;
    private View view;
    private PageRecyclerView.PageAdapter myAdapter = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_sport, container, false);
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }


        return view;

    }

    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        mRecyclerView = (PageRecyclerView) view.findViewById(R.id.cusom_swipe_view);
        // 设置指示器
        //  mRecyclerView.setIndicator((PageIndicatorView) findViewById(R.id.indicator));
        // 设置行数和列数
        mRecyclerView.setPageSize(1, 1);
        // 设置页间距
//        mRecyclerView.setPageMargin(30);
        // 设置数据
        mRecyclerView.setAdapter(mRecyclerView.new PageAdapter(initData(), new PageRecyclerView.CallBack() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(getContext()).inflate(R.layout.item_sport_hengxiang, parent, false);

                return new ViewHolder(view);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
                ViewHolder viewHolder = (ViewHolder) holder;
                if(position == 0){

                    Glide.with(getContext()).load(R.mipmap.common).into( viewHolder.myTitle_Image);
                    Glide.with(getContext()).load(R.drawable.common_u).into(viewHolder.myImage1);
                    Glide.with(getContext()).load(R.drawable.common_d).into( viewHolder.myImage2);
                    viewHolder.left_icon.setVisibility(View.GONE);
                    viewHolder.right_icon.setVisibility(View.VISIBLE);
                }else if(position == 1){
                    Glide.with(getContext()).load(R.mipmap.district).into( viewHolder.myTitle_Image);
                    Glide.with(getContext()).load(R.drawable.district_u).into( viewHolder.myImage1);
                    Glide.with(getContext()).load(R.drawable.district_d).into(  viewHolder.myImage2);
                    viewHolder.left_icon.setVisibility(View.VISIBLE);
                    viewHolder.right_icon.setVisibility(View.VISIBLE);
                }else if(position == 2){
                    Glide.with(getContext()).load(R.mipmap.graph).into( viewHolder.myTitle_Image);
                    Glide.with(getContext()).load(R.drawable.graph_u).into(viewHolder.myImage1);
                    Glide.with(getContext()).load(R.drawable.graph_d).into(viewHolder.myImage2);
                    viewHolder.left_icon.setVisibility(View.VISIBLE);
                    viewHolder.right_icon.setVisibility(View.GONE);
                }
//                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if (position==1){
//                            RegionActivity.openActivity(getContext());
//
//                        }else if (position==2){
//                            Intent intent = new Intent(getContext(), FavouritePicActivity.class);
//                            intent.putExtra("position", 0);
//                            getContext().startActivity(intent);
//                        }
//                    }
//                });
                viewHolder.myImage1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(position == 0){          //室内常规跑步
                            Intent intent = new Intent(getContext(), SportHomeActivity.class);
                            intent.putExtra("disNum", 0);
                            getContext().startActivity(intent);
                        }else if(position == 1){//行政区室内
                            RegionActivity.openActivity(getContext(),2);

                        }else if(position == 2){
                            Intent intent = new Intent(getContext(), FavouritePicActivity.class);
                            intent.putExtra("position", 0);
                            getContext().startActivity(intent);

                        }
                    }
                });
                viewHolder.myImage2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (position==0){
                            Intent intent = new Intent(getContext(), NormalOutdoorRunActivity.class);
                            intent.putExtra("disNum", 0);
                            getContext().startActivity(intent);
                        }else if (position==1){//行政室外
                            RegionActivity.openActivity(getContext(),3);
                        }else if (position==2){
                            Intent intent = new Intent(getContext(), FavouritePicActivity.class);
                            intent.putExtra("position", 1);
                            getContext().startActivity(intent);
                        }
                    }
                });
            }
        }));
    }
    public List<Integer> initData(){
        List<Integer> list=new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            list.add(i);
        }
        return list;
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView myTitle_Image = null;
        public ImageView myImage1 = null;
        public ImageView myImage2 = null;

        public ImageView left_icon = null;
        public ImageView right_icon = null;
        public ViewHolder(View itemView) {
            super(itemView);
            myTitle_Image = (ImageView) itemView.findViewById(R.id.myTitle_Image);
            myImage1 = (ImageView) itemView.findViewById(R.id.myImage1);
            myImage2 = (ImageView) itemView.findViewById(R.id.myImage2);

            left_icon = (ImageView) itemView.findViewById(R.id.left_icon);
            right_icon = (ImageView) itemView.findViewById(R.id.right_icon);
        }
}

}